/**
 * 2025/01/01 - 2025/12/31
 */
package com.snail.server.iam.app.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snail.server.iam.app.entity.App;
import com.snail.server.iam.app.mapper.AppMapper;
import com.snail.server.iam.error.IamErrorCode;
import com.snail.starter.commons.exception.BizException;
import com.snail.starter.db.enums.EnableOrDisEnableEnum;
import org.springframework.stereotype.Service;

/**
 * @author zgadmin
 * @date 2025/9/24 19:11
 */
@Service
public class AppService extends ServiceImpl<AppMapper, App> {

    public App checkAppByKey(String appKey) {
        return check(getOne(Wrappers.lambdaQuery(App.class).eq(App::getAppKey, appKey)));
    }

    public App checkApp(Long appId) {
        return check(getOne(Wrappers.lambdaQuery(App.class).eq(App::getAppId, appId)));
    }

    private App check(App app) {
        if (null == app) {
            throw BizException.of(IamErrorCode.APP_NOT_EXISTS_NO_PARAM, new Object[]{app});
        }
        if (!EnableOrDisEnableEnum.ENABLE.equals(app.getStatus())) {
            throw BizException.of(IamErrorCode.APP_DISABLE);
        }
        return app;
    }
}